package com.wing.res.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.enums.ProductEnum;
import com.wing.res.dao.ResProductQualityTemplateDao;

import com.wing.res.model.entity.ResProductQualityTemplate;
import com.wing.res.model.entity.ResProductQualityTemplateParam;
import com.wing.res.model.response.ResProductQualityTemplateParamVo;
import com.wing.res.model.response.ResProductQualityTemplateSearchVo;
import com.wing.res.service.ResProductQualityTemplateParamService;
import com.wing.res.service.ResProductQualityTemplateService;
import com.wing.web.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 质检模板-ServiceImpl
 * @version 1.1.0
 * @Author hz
 * @Date 2024-09-23 21:32
 **/
@Service
public class ResProductQualityTemplateServiceImpl extends ServiceImpl<ResProductQualityTemplateDao, ResProductQualityTemplate> implements ResProductQualityTemplateService {


    @Autowired
    private ResProductQualityTemplateParamService productQualityTemplateParamService;


    /**
     * 获取模板列表
     * @param pg
     * @param map
     * @return
     */
    @Override
    public Page<ResProductQualityTemplate> pageList4app(Page<ResProductQualityTemplate> pg, Map map) {
        List<ResProductQualityTemplate> list = baseMapper.list4app(pg, map);
        pg.setRecords(list);
        return pg;
    }

    /**
     * 添加模板
     * @param templateVo
     */
    @Override
    @Transactional
    public void addTemplate(ResProductQualityTemplateSearchVo templateVo) {
        //添加模板id
        ResProductQualityTemplate template = new ResProductQualityTemplate();
        if (ObjectUtil.isNotEmpty(template)){
            BeanUtil.copyProperties(templateVo,template);
        }
        template.setCreateTime(LocalDateTime.now());
        template.setCreatorName(JwtUtils.getLoginUsername());
        template.setCreator(JwtUtils.getLoginUserId());
        boolean save = this.save(template);
        if (save){
            List<ResProductQualityTemplateParamVo> paramVoList = templateVo.getParamVoList();
            List<ResProductQualityTemplateParam> list = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(paramVoList)){
                for (ResProductQualityTemplateParamVo vo : paramVoList) {
                    ResProductQualityTemplateParam param = new ResProductQualityTemplateParam();
                    param.setParamId(vo.getId());
                    param.setCreateTime(LocalDateTime.now());
                    param.setCreatorName(JwtUtils.getLoginUsername());
                    param.setCreator(JwtUtils.getLoginUserId());
                    param.setTemplateId(template.getId());
                    list.add(param);
                }
            }
            this.productQualityTemplateParamService.saveBatch(list);
        }
    }

    /**
     * 修改
     * @param template
     * @return
     */
    @Override
    @Transactional
    public int updateTemplate(ResProductQualityTemplateSearchVo template) {
        Long templateId = template.getId();
        ResProductQualityTemplate byId = this.getById(templateId);
        if (ObjectUtil.isEmpty(byId)){
            return 0;
        }
        BeanUtil.copyProperties(template,byId);
        byId.setUpdateTime(LocalDateTime.now());
        byId.setModifier(JwtUtils.getLoginUserId());
        byId.setModifierName(JwtUtils.getLoginUsername());
        boolean b = this.updateById(byId);
        if (b){
            QueryWrapper<ResProductQualityTemplateParam> wrapper = new QueryWrapper<>();
            wrapper.eq("template_id",byId.getId());
            boolean remove = this.productQualityTemplateParamService.remove(wrapper);
            if (remove){
                List<ResProductQualityTemplateParamVo> paramVoList = template.getParamVoList();
                List<ResProductQualityTemplateParam> list = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(paramVoList)){
                    for (ResProductQualityTemplateParamVo vo : paramVoList) {
                        ResProductQualityTemplateParam param = new ResProductQualityTemplateParam();
                        param.setParamId(vo.getId());
                        param.setCreateTime(LocalDateTime.now());
                        param.setCreatorName(JwtUtils.getLoginUsername());
                        param.setCreator(JwtUtils.getLoginUserId());
                        param.setTemplateId(template.getId());
                        list.add(param);
                    }
                }
                boolean saveBatch = this.productQualityTemplateParamService.saveBatch(list);
                if (saveBatch){
                    return 1;
                }
            }
        }
        return 0;
    }

    /**
     *
     * 查询详情
     * @param templateId
     * @return
     */
    @Override
    public ResProductQualityTemplateSearchVo getDetailBy(Long templateId) {
        ResProductQualityTemplateSearchVo searchVo = new ResProductQualityTemplateSearchVo();
        ResProductQualityTemplate byId = this.getById(templateId);
        if (ObjectUtil.isNotEmpty(byId)){
            BeanUtil.copyProperties(byId,searchVo);
        }
        //查询质检参数
        List<ResProductQualityTemplateParamVo> list = productQualityTemplateParamService.listParamsBy(templateId);
        searchVo.setParamVoList(list);
        return searchVo;
    }

    /**
     * 废除 or 恢复
     * @param templateId
     * @param status
     * @return
     */
    @Override
    public int disableOrOpen(Long templateId, String status) {
        ResProductQualityTemplate byId = this.getById(templateId);
        if (ObjectUtil.isEmpty(byId)){
            return 0;
        }
        if (ProductEnum.templateStatusEmum.FC.getCode().equals(status)){
            byId.setIsDeleted(true);
        }else{
            byId.setIsDeleted(false);
        }
        byId.setUpdateTime(LocalDateTime.now());
        byId.setModifier(JwtUtils.getLoginUserId());
        byId.setModifierName(JwtUtils.getLoginUsername());
        boolean b = this.updateById(byId);
        if (b){
            return 1;
        }
        return 0;
    }
}
